package cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.goods.service.goods;

import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.goods.controller.admin.goods.vo.GoodSpecif;
import cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.goods.controller.admin.goods.vo.GoodSpecifChild;
import cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.goods.controller.admin.goods.vo.GoodsExportReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.goods.controller.admin.goods.vo.GoodsPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.goods.dal.dataobject.goods.GoodsDO;
import cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.goods.dal.mysql.goods.GoodsMapper;
import cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.goods.util.PersonComparator;
import com.aliyuncs.utils.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.math.BigDecimal;
import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;


import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 小程序-商品 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class GoodsServiceImpl implements GoodsService {

    @Resource
    private GoodsMapper goodsMapper;

    @Override
    public Integer createGoods(GoodsDO goods) {

        //普通商品添加
        if (goods.getOneName()!=null){
            if (goods.getOneImg()==null)
                throw new ServiceException(500,"sku图片不能为空");
            if (goods.getOnePrice()==null)
                throw new ServiceException(500,"商品价格不能为空");
            if (goods.getOneSize()==null)
                throw new ServiceException(500,"库存不能为空");
            List<GoodSpecif> specif=new ArrayList<>();
            GoodSpecif goodSpecif = new GoodSpecif();
            goodSpecif.setId(UUID.randomUUID().toString());
            goodSpecif.setName(goods.getOneName());
            goodSpecif.setUrl(goods.getOneImg());
            List<GoodSpecifChild> child=new ArrayList<>();
            GoodSpecifChild specifChild = new GoodSpecifChild();
            specifChild.setId(UUID.randomUUID().toString());
            specifChild.setSize("默认");
            specifChild.setPrice(goods.getOnePrice());
            specifChild.setNum(goods.getOneSize());
            child.add(specifChild);
            goodSpecif.setChild(child);
            specif.add(goodSpecif);

            goods.setMinPrice(goods.getOnePrice());
            goods.setSpecif(specif);


        }else {
            //有颜色尺码的商品添加
            List<BigDecimal> list=new ArrayList<>();
            for (GoodSpecif specif : goods.getSpecif()) {
                specif.setId(UUID.randomUUID().toString());
                for (GoodSpecifChild child : specif.getChild()) {
                    child.setId(UUID.randomUUID().toString());
                    list.add(child.getPrice());
                }
            }
            list.sort(new PersonComparator());
            if (list.size()>0)
                goods.setMinPrice(list.get(0));
        }


        // 插入
        try {
            goodsMapper.insert(goods);
        }catch (Exception e){
            if (e.getMessage().contains("qd_goods.code")){
                throw new ServiceException(500,"编码重复");
            }
            throw new ServiceException(500,"后端异常");
        }
        // 返回
        return goods.getId();
    }

    @Override
    public void updateGoods(GoodsDO goods) {
        // 校验存在
        validateGoodsExists(goods.getId());

        List<BigDecimal> list=new ArrayList<>();
        for (GoodSpecif specif : goods.getSpecif()) {
            if (StringUtils.isEmpty(specif.getId()))
                specif.setId(UUID.randomUUID().toString());
            if (specif.getChild()!=null&&specif.getChild().size()==0)
                throw new ServiceException(500,specif.getName()+"的规格没有添加尺码价格");
            for (GoodSpecifChild child : specif.getChild()) {
                checkSpecifSize(child);
                list.add(child.getPrice());
                if (StringUtils.isEmpty(child.getId()))
                    child.setId(UUID.randomUUID().toString());
            }
        }
        list.sort(new PersonComparator());
        if (list.size()>0)
            goods.setMinPrice(list.get(0));
        // 更新
        try {
            goodsMapper.updateById(goods);
        }catch (Exception e){
            if (e.getMessage().contains("qd_goods.code")){
                throw new ServiceException(500,"编码重复");
            }
            throw new ServiceException(500,"后端异常");
        }
    }

    public void checkSpecifSize(GoodSpecifChild vo){
        if (vo.getSize()==null)
            throw new ServiceException(500,"尺码不能为空");
        if (vo.getPrice()==null)
            throw new ServiceException(500,"价格不能为空");
        if (vo.getNum()==null)
            throw new ServiceException(500,"库存数量不能为空");
    }

    @Override
    public void deleteGoods(Integer id) {
        // 校验存在
        validateGoodsExists(id);
        // 删除
        goodsMapper.deleteById(id);
    }

    private void validateGoodsExists(Integer id) {
        if (goodsMapper.selectById(id) == null) {
            throw exception(500);
        }
    }

    @Override
    public GoodsDO getGoods(Integer id) {
        GoodsDO goodsDO = goodsMapper.selectById(id);
        if (goodsDO.getAttributes()==null)
            goodsDO.setAttributes(new ArrayList<>());
        return goodsDO;
    }

    @Override
    public List<GoodsDO> getGoodsList(Collection<Integer> ids) {
        return goodsMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<GoodsDO> getGoodsPage(GoodsPageReqVO pageReqVO) {
        return goodsMapper.selectPage(pageReqVO);
    }

    @Override
    public List<GoodsDO> getGoodsList(GoodsExportReqVO exportReqVO) {
        return goodsMapper.selectList(exportReqVO);
    }

}
